package com.icewld.alarmofremindingme;

import android.app.Application;
import android.content.Context;
import android.media.MediaPlayer;
import android.provider.MediaStore;
import android.widget.Toast;

import java.util.LinkedList;
import java.util.Queue;

public class SoundMaker {

    private MediaPlayer[] sound;
    private int sound_length = 16;
    private boolean isPlay;
    private Queue<String> soundqueue;

    public SoundMaker(final Context context){
        isPlay = false;
        soundqueue = new LinkedList<String>();
        sound = new MediaPlayer[sound_length];
        sound[2] = MediaPlayer.create(context, R.raw.getup);
        sound[1] = MediaPlayer.create(context, R.raw.enjoy);
        sound[0] = MediaPlayer.create(context, R.raw.learn);
        sound[3] = MediaPlayer.create(context, R.raw.d);
        sound[4] = MediaPlayer.create(context, R.raw.ds);
        sound[5] = MediaPlayer.create(context, R.raw.en);
        sound[6] = MediaPlayer.create(context, R.raw.gr);
        sound[7] = MediaPlayer.create(context, R.raw.jz);
        sound[8] = MediaPlayer.create(context, R.raw.math);
        sound[9] = MediaPlayer.create(context, R.raw.net);
        sound[10] = MediaPlayer.create(context, R.raw.os);
        sound[11] = MediaPlayer.create(context, R.raw.p);
        sound[12] = MediaPlayer.create(context, R.raw.x);
        sound[13] = MediaPlayer.create(context, R.raw.zz);
        sound[14] = MediaPlayer.create(context, R.raw.zm);
        sound[15] = MediaPlayer.create(context, R.raw.birth);

        for (int i = 0; i < sound_length; i++){
            sound[i].setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    isPlay = false;
                    if (!soundqueue.isEmpty()) {
                        sound[Integer.parseInt(soundqueue.poll())].start();
                        isPlay = true;
                    }
                }
            });
        }
    }

    public void MakeSound(String type){
        int sound_id = -1;
        System.out.println(String.valueOf(isPlay));
        switch (type) {
            case "enjoy":
                sound_id = 1;
                break;
            case "learn":
                sound_id = 0;
                break;
            case "getup":
                sound_id = 2;
                break;
            case "D":
                sound_id = 3;
                break;
            case "DS":
                sound_id = 4;
                break;
            case "En":
                sound_id = 5;
                break;
            case "GR":
                sound_id = 6;
                break;
            case "JZ":
                sound_id = 7;
                break;
            case "Math":
                sound_id = 8;
                break;
            case "Net":
                sound_id = 9;
                break;
            case "OS":
                sound_id = 10;
                break;
            case "P":
                sound_id = 11;
                break;
            case "X":
                sound_id = 12;
                break;
            case "ZZ":
                sound_id = 13;
                break;
            case "ZM":
                sound_id = 14;
                break;
            case "birth":
                sound_id = 15;
                break;
            default:
                break;
        }
        if(!isPlay) {
            if (sound_id != -1) {
                sound[sound_id].start();
                isPlay = true;
            }
        }
        else {
            if (sound_id != -1) {
                soundqueue.offer(String.valueOf(sound_id));
            }
        }
    }

}
